package com.lesso.yjy.sunflower.core;

import lombok.Getter;

import java.io.Serializable;

@Getter
public class ApiResponse<T> implements Serializable {
    private static final long serialVersionUID = 20250703L;

    public static final int SUCCESS = 1;

    public static final int ERROR = 0;

    private final int code;

    private final String msg;

    private final T data;

    private ApiResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Builder<T> ok() {
        return new Builder<>(SUCCESS);
    }

    public static <T> Builder<T> error() {
        return new Builder<>(ERROR);
    }

    public static final class Builder<T> {
        private int code;
        private String msg;
        private T data;

        private Builder(int code) {
            this.code = code;
        }

        public Builder<T> code(int code) {
            this.code = code;
            return this;
        }

        public Builder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder<T> data(T data) {
            this.data = data;
            return this;
        }

        public ApiResponse<T> build() {
            return new ApiResponse<>(code, msg, data);
        }
    }
}